package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.common.utils.Common;
import com.think.teaching.mapper.QuestionMapper;
import com.think.teaching.model.Question;
import com.think.teaching.model.QuestionSelection;
import com.think.teaching.service.QuestionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试题 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class QuestionServiceImpl extends BaseServiceImpl<QuestionMapper, Question> implements QuestionService {
	
	private final QuestionSelectionServiceImpl questionSelectionServiceImpl;

	public QuestionServiceImpl(QuestionSelectionServiceImpl questionSelectionServiceImpl) {
		this.questionSelectionServiceImpl = questionSelectionServiceImpl;
	}

	@Override
	@Transactional
	public Question AddQuestion(Question question) throws Exception {
		question.setCreatedBy(Common.GetCurrentAuthor().getUserId());
		if (this.save(question)) {
			if (question.getType() == Question.Multiple_Choice) {
				List<QuestionSelection> questionSelections = new ArrayList<>();
				for (int i = 0; i < question.getSelections().size(); i++) {
					QuestionSelection q = new QuestionSelection();
					q.setQuestionId(question.getId());
					q.setContent(question.getSelections().get(i));
					q.setCreatedBy(question.getCreatedBy());
					questionSelections.add(q);
				}
				if(!questionSelectionServiceImpl.saveBatch(questionSelections)) {
					throw new Exception("存入选项失败");
				}
			}
			
			return question;
		}
		return null;
	}


	@Override
	@Transactional
	public Question UpdateQuestion(Question question) throws Exception {
		question.setCreatedBy(Common.GetCurrentAuthor().getUserId());
		if (this.updateById(question)) {
			UpdateWrapper<QuestionSelection> queryWrapper = new UpdateWrapper<>();
			queryWrapper.eq("question_id", question.getId());
			questionSelectionServiceImpl.remove(queryWrapper);
			List<QuestionSelection> questionSelections = new ArrayList<>();
			for (int i = 0; i < question.getSelections().size(); i++) {
				QuestionSelection q = new QuestionSelection();
				q.setQuestionId(question.getId());
				q.setContent(question.getSelections().get(i));
				q.setCreatedBy(question.getCreatedBy());
				questionSelections.add(q);
			}
			questionSelectionServiceImpl.saveBatch(questionSelections);
			return question;
		}
		return null;
	}

	public Map<String, Object> GetQuestion(Integer id,Boolean withoutBan) {
//		QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("id", id).ne("delflag", 1);
//		if (withoutBan) {
//			queryWrapper.eq("state", 1);
//		}
		return baseMapper.GetQuestionDetails(id);
	}

	@Override
	public Question SetQuestionState(Integer id, Integer state) {
		UpdateWrapper<Question> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	@Override
	public IPage<Map<String, Object>> ListQuestionWithTagAndType(Integer count, Integer page
			, Integer type, Integer tag, Boolean withoutBan,Integer courseId) {
		IPage<Map<String, Object>> ipage = new Page<>(page, count);
		return baseMapper.ListQuestionWithTagAndType(ipage,type,withoutBan,courseId);
	}

	public List<Question> SearchQuestionWithKey(String key, Boolean withoutBan,Integer id,Integer type) {
		QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("content", key);
		if(type>0)
		queryWrapper.eq("type", type);
		if(id>0)
		queryWrapper.eq("course_id", id);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	@Override
	public List<QuestionSelection> ListQuestionSelection(Integer id, Boolean withoutBan) {
		// TODO Auto-generated method stub
		return null;
	}

}
